﻿using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;

public class SqliteReader
{
    private static SqliteReader self;

    private string openedDBName;

    private DbAccess db;

    public SqliteReader() { }

    public static SqliteReader getInstance()
    {
        if (self == null)
        {
            self = new SqliteReader();
        }
        return self;
    }

    /// <summary>
    /// 由Sqlite表,取对应的配置文件
    /// </summary>
    /// <param name="tableName">表名</param>
    /// <param name="keyName">key名</param>
    /// <param name="keyValue">key值</param>
    /// <param name="args">所要取的参数</param>
    /// <param name="isClose">操作完是否关闭数据库连接</param>
    /// <returns>返回所要取的参数对应的值</returns>
    public string[] getIniVal(string tableName, string keyName, string keyValue, bool needClose, params string[] args)
    {
        try
        {
            string appDBPath = Application.dataPath + ResPath.DBPath;


            if (openedDBName != appDBPath)
            {
                openedDBName = appDBPath;
                db = new DbAccess(@"Data Source=" + appDBPath);
            }

            string[] strArgs = new string[args.Length];
            using (SqliteDataReader sqReader = db.SelectWhere(tableName, args, new string[] { keyName }, new string[] { "=" }, new string[] { keyValue }))
            {
                while (sqReader.Read())
                {
                    //目前中文无法显示
                    for (int i = 0; i < strArgs.Length; i++)
                    {
                        int n = sqReader.GetOrdinal(args[i]);
                        strArgs[i] = n != -1 ? sqReader.GetValue(n).ToString() : "";
                    }
                }
                sqReader.Close();
            }
            if (needClose)
            {
                db.CloseSqlConnection();
                db = null;
                openedDBName = "";
            }

            return strArgs;
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex + "SqliteReader数据库操作出错");
            string[] tmp = new string[args.Length];
            for (int i = 0; i < tmp.Length; i++)
            {
                tmp[i] = "";
            }
            return tmp;
        }

    }
    /// <summary>
    /// 由Sqlite表,取对应的配置文件
    /// </summary>
    /// <param name="tableName">表名</param>
    /// <param name="keyName">key名</param>
    /// <param name="keyValue">key值</param>
    /// <param name="args">所要取的参数</param>
    /// <returns>返回所要取的参数对应的值</returns>
    public string[] getIniVal(string tableName, string keyName, string keyValue, params string[] args)
    {
        return getIniVal(tableName, keyName, keyValue, false, args);
    }

    /// <summary>
    /// 由Sqlite表,取该行所有参数的值。
    /// </summary>
    /// <param name="tableName">表名</param>
    /// <param name="keyName">key名</param>
    /// <param name="keyValue">key值</param>
    /// <param name="isClose">操作完是否关闭数据库连接</param>
    /// <returns>返回所要取的参数对应的值</returns>
    public string[] getIniVal(string tableName, string keyName, string keyValue, bool needClose)
    {
        try
        {
            string appDBPath = Application.dataPath + ResPath.DBPath;

            if (openedDBName != appDBPath)
            {
                openedDBName = appDBPath;
                db = new DbAccess(@"Data Source=" + appDBPath);
            }

            List<string> strList = new List<string>();
            using (SqliteDataReader sqReader = db.SelectAll(tableName, new string[] { keyName }, new string[] { "=" }, new string[] { keyValue }))
            {
                while (sqReader.Read())
                {
                    //目前中文无法显示
                    for (int i = 0; i < sqReader.FieldCount; i++)
                    {
                        strList.Add(sqReader.GetValue(i).ToString());
                    }
                }
                sqReader.Close();
            }
            if (needClose)
            {
                db.CloseSqlConnection();
                db = null;
                openedDBName = "";
            }

            string[] strArgs = strList.ToArray();
            return strArgs;
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex + "SqliteReader数据库操作出错");
            return null;
        }
    }

    /// <summary>
    /// 取得表的所有数据.
    /// </summary>
    /// <returns>The all.</returns>
    /// <param name="表名">Table name.</param>
    public SqliteDataReader SelectAll(string tableName)
    {
        try
        {
            string appDBPath = Application.dataPath + ResPath.DBPath;

            Debug.Log("appDBPath" + Application.dataPath);
            if (openedDBName != appDBPath)
            {
                openedDBName = appDBPath;
                db = new DbAccess(@"Data Source=" + appDBPath);
            }

            return db.ReadFullTable(tableName);
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex + "SqliteReader数据库操作出错");
            return null;
        }
    }

    /// <summary>
    /// 取得表某些列的所有数据.
    /// </summary>
    /// <param tableName="表名">表名.</param>
    public SqliteDataReader SelectAll(string tableName, string[] orderStr, params string[] args)
    {
        try
        {
            string appDBPath = Application.dataPath + ResPath.DBPath;

            Debug.Log("appDBPath" + Application.dataPath);
            if (openedDBName != appDBPath)
            {
                openedDBName = appDBPath;
                db = new DbAccess(@"Data Source=" + appDBPath);
            }

            return db.SelectAll(tableName, args, orderStr);
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex + "SqliteReader数据库操作出错");
            return null;
        }
    }
}